package org.bilinc.gz001.dao;


import org.bilinc.gz001.dao.interfaces.AbstractDaoImpl;
import org.bilinc.gz001.dao.interfaces.IResourceDao;
import org.bilinc.gz001.entity.Position;
import org.bilinc.gz001.entity.Resource;
import org.bilinc.gz001.hibernateUtil.HibernateUtil;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class ResourceDao extends AbstractDaoImpl<Resource> implements IResourceDao {

    @Autowired
    HibernateUtil hibernateUtil;

    private final static String HQL_GET_RES_BU_POSITION = "SELECT r FROM Resource r where r.position=:positionId";
    private final static String HQL_GET_SIZE = "SELECT COUNT(r) FROM Resource r";

    ResourceDao() {}

    public List<Resource> getListOfPosition(Position position) {

        List<Resource> resources = new ArrayList<Resource>();
        if(position!= null) {
            resources =  getSession().createQuery(HQL_GET_RES_BU_POSITION)
                    .setLong("positionId", position.getId())
                    .list();
        }
        for (Resource resource : resources) {
            if (resource.getCity() != null) {
                Hibernate.initialize(resource.getCity());
            }
            if (resource.getPosition() != null) {
                Hibernate.initialize(resource.getPosition());
            }
        }
        closeSession();
        return resources;
    }

    public Long getSize() {
        Long size =  (Long) getSession().createQuery(HQL_GET_SIZE).uniqueResult();
        closeSession();
        return size;
    }

    public List<Resource> getAll() {
        List<Resource> resources = (List<Resource>) getSession().createCriteria(Resource.class).list();
        closeSession();
        return resources;
    }

    public Resource findByFullName(String fullName) {
        Resource resource =  (Resource) createCriteriaNoTransaction().add(Restrictions.eq("fullName", fullName)).uniqueResult();
        if (resource.getCity() != null) {
            Hibernate.initialize(resource.getCity());
        }
        if (resource.getPosition() != null) {
            Hibernate.initialize(resource.getPosition());
        }
        closeSession();
        return resource;
    }

    public Resource findByTanelNumber(String number) {
        Resource resource =  (Resource) getSession().createCriteria(Resource.class).add(Restrictions.eq("tabNumber", number)).uniqueResult();
        if (resource.getCity() != null) {
            Hibernate.initialize(resource.getCity());
        }
        if (resource.getPosition() != null) {
            Hibernate.initialize(resource.getPosition());
        }
        closeSession();
        return resource;
    }

    public void removeByTanelNumber(String number) {
        Session session = getSessionStartTransaction();
        Resource resource = (Resource) createCriteriaInTransaction().add(Restrictions.eq("tabNumber", number)).uniqueResult();
        session.delete(resource);
        commitTransaction();
    }
}
